﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Services.Transaction;
using ZDSoft.LMS.Service;
using ZDSoft.LMS.Domain;
using ZDSoft.LMS.Core;
using ZDSoft.LMS.Manager;
namespace ZDSoft.LMS.Component
{
    public class SystemSettingComponent : BaseComponent<SystemSetting,SystemSettingManager>,ISystemSettingService
    {
        public void InitDataBase()//初始化数据库
        {
            CreateBaseData();//创建基础数据
        }

        private void CreateBaseData()
        {
            CreateSystemFunction();//创建系统功能列表
            CreateUser();//创建用户
            CreateRole();//创建角色            

            CreateDictionaries();//创建字典

            CreateStation();
            CreateDefaultSystemSetting();//创建系统设置
            //测试数据
            CreateDriver();
            CreateVehicle();

        }

        IList<Station> stationList = null;

        private void CreateStation()
        {
            stationList = new List<Station>() { 
                new Station(){ Name="龙头寺",Province = provinceList[0]},
                new Station(){ Name="菜园坝火车票",Province = provinceList[0]},
                new Station(){ Name="北碚火车票",Province = provinceList[0]},
                new Station(){ Name="南坪汽车站",Province = provinceList[0]},
                
                new Station(){ Name="荷花池",Province = provinceList[2]},
                new Station(){ Name="成都东站",Province = provinceList[2]},
                new Station(){ Name="成都汽车站",Province = provinceList[2]},
                new Station(){ Name="都江堰站",Province = provinceList[2]},
            };
            StationComponent component = new StationComponent();
            foreach (Station entity in stationList)
            {
                component.Create(entity);
            }
        }

        private void CreateDefaultSystemSetting()
        {
            SystemSetting setting = new SystemSetting();
            setting.DataBaseVersion = 1;
            setting.DefaultPassword = "202CB962AC59075B964B07152D234B70";

            Create(setting);//调用基类执行
        }

        IList<SystemFunction> functionList = null;


        private void CreateSystemFunction()
        {
            this.functionList = new List<SystemFunction>();//实例化系统功能集合

            this.functionList.Add(new SystemFunction() { 
                SystemFunctionName = "控制面板", 
                ClassName = "", 
                Idx = 100 });//添加控制面板功能
            this.functionList.Add(new SystemFunction() { 
                SystemFunctionName = "用户管理", 
                ControllerName = "User", 
                ActionName = "Index", 
                ClassName = "ZDSoft.LMS.Web.Controllers.UserController", 
                Parent = this.functionList[0] });//添加用户管理功能，并设置父级菜单是控制面板
                this.functionList.Add(new SystemFunction() { SystemFunctionName = "角色管理", ControllerName = "Role",
               ActionName = "Index", ClassName = "ZDSoft.LMS.Web.Controllers.RoleController", 
             Parent = this.functionList[0] });//添加角色管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "权限管理", ControllerName = "Authorize", ActionName = "Index", ClassName = "ZDSoft.LMS.Web.Controllers.AuthorizeController",
                Parent = this.functionList[0] });//添加权限管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "修改密码", ControllerName = "User", ActionName = "ChangePassword", ClassName = "ZDSoft.LMS.Web.Controllers.UserController", Parent = this.functionList[0] });//添加权限管理功能，并设置父级菜单是控制面板
            
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "首页", ControllerName = "Home",Idx = 1, ActionName = "Index", ClassName = "ZDSoft.LMS.Web.Controllers.HomeController" });//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "业务管理", ClassName="",Idx = 10});//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "接单管理", ControllerName = "TransportOrder", ActionName = "Index", ClassName = "ZDSoft.LMS.Web.Controllers.TransportOrderController", Parent = this.functionList[6] });//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "调度管理", ControllerName = "Dispatch", ActionName = "Index", ClassName = "ZDSoft.LMS.Web.Controllers.DispatchController", Parent = this.functionList[6] });//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "业务跟踪", ControllerName = "Trace", ActionName = "Index", ClassName = "ZDSoft.LMS.Web.Controllers.TraceController", Parent = this.functionList[6] });//添加用户管理功能，并设置父级菜单是控制面板
            
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "送货单管理", ControllerName = "DeliveryForm", ActionName = "Index", ClassName = "ZDSoft.LMS.Web.Controllers.DeliveryFormController", Parent = this.functionList[6] });//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "回车报销", ControllerName = "Billing", ActionName = "Index", ClassName = "ZDSoft.LMS.Web.Controllers.BillingController", Parent = this.functionList[6] });//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "决策分析", ClassName="",Idx = 80});//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "创建货物运单", ControllerName = "TransportOrder", ActionName = "Create", ClassName = "ZDSoft.LMS.Web.Controllers.TransportOrderController", Parent = this.functionList[6] });//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "报表管理", ClassName = "",Idx = 60 });//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "创建月报表", ControllerName = "Report", ActionName = "CreateMonthly", ClassName = "ZDSoft.LMS.Web.Controllers.ReportController", Parent = this.functionList[14] });//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "创建年报表", ControllerName = "Report", ActionName = "CreateYearly", ClassName = "ZDSoft.LMS.Web.Controllers.ReportController", Parent = this.functionList[14] });//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "工资管理", ControllerName = "Salary",Idx = 70, ActionName = "Index", ClassName = "ZDSoft.LMS.Web.Controllers.SalaryController" });//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "月度分析", ControllerName = "Analysis", ActionName = "MonthlyAnalysis", ClassName = "ZDSoft.LMS.Web.Controllers.AnalysisController",Parent=this.functionList[12] });//添加用户管理功能，并设置父级菜单是控制面板
            this.functionList.Add(new SystemFunction() { SystemFunctionName = "季度分析", ControllerName = "Analysis", ActionName = "SeasonAnalysis", ClassName = "ZDSoft.LMS.Web.Controllers.AnalysisController", Parent = this.functionList[12] });//添加用户管理功能，并设置父级菜单是控制面板

            SystemFunctionComponent component = new SystemFunctionComponent();//实例化SystemFunctionComponent

            foreach (SystemFunction function in this.functionList)//添加所有系统功能
            {
                component.Create(function);
            }
        }

        IList<Role> roleList = null;
        private void CreateRole()
        {
            //业务员 ,调度员，财务人员，系统管理员，超级用户
            roleList = new List<Role>();//实例化角色集合

            roleList.Add(new Role()
            {
                RoleName = "业务员",//设置角色名称
                IsActive = true,//设置为“激活”
                Users = new List<User>() { this.userList[3] },//为当前角色添加用户
                SystemFunctions = new List<SystemFunction>() 
                { this.functionList[0], this.functionList[4], this.functionList[5], this.functionList[6], this.functionList[7], this.functionList[13]}//为当前角色添加权限
            });

            roleList.Add(new Role()
            {
                RoleName = "调度员",//设置角色名称
                IsActive = true,//设置为“激活”
                Users = new List<User>() { this.userList[4] },//为当前角色添加用户
                SystemFunctions = new List<SystemFunction>() { this.functionList[0], this.functionList[4], this.functionList[5], this.functionList[8], this.functionList[9]}//为当前角色添加权限
            });

            roleList.Add(new Role()
            {
                RoleName = "业务员",//设置角色名称
                IsActive = true,//设置为“激活”
                Users = new List<User>() { this.userList[3] },//为当前角色添加用户
                SystemFunctions = new List<SystemFunction>() { this.functionList[0], this.functionList[4], this.functionList[5], this.functionList[6], this.functionList[7], this.functionList[13] }//为当前角色添加权限
            });

            roleList.Add(new Role()
            {
                RoleName = "财务人员",//设置角色名称
                IsActive = true,//设置为“激活”
                Users = new List<User>() { this.userList[6] },//为当前角色添加用户
                SystemFunctions = new List<SystemFunction>() { this.functionList[0], this.functionList[4], this.functionList[5], this.functionList[11], this.functionList[15], this.functionList[16], this.functionList[17] }//为当前角色添加权限
            });

            roleList.Add(new Role()
            {
                RoleName = "系统管理员",//设置角色名称
                IsActive = true,//设置为“激活”
                Users = new List<User>() { this.userList[0] },//为当前角色添加用户
                SystemFunctions = new List<SystemFunction>() { this.functionList[0], this.functionList[1], this.functionList[2], this.functionList[3], this.functionList[4], this.functionList[5], this.functionList[6] }//为当前角色添加权限
            });

            roleList.Add(new Role()
            {
                RoleName = "超级用户",//设置角色名称
                IsActive = true,//设置为“激活”
                Users = new List<User>() { this.userList[1] },//为当前角色添加用户
                SystemFunctions = new List<SystemFunction>() { this.functionList[0], this.functionList[1], this.functionList[2], this.functionList[3], this.functionList[4], this.functionList[5], this.functionList[6], this.functionList[7], this.functionList[8], this.functionList[9], this.functionList[10], this.functionList[11], this.functionList[12] }//为当前角色添加权限
            });

            RoleComponent component = new RoleComponent();//实例化RoleComponent
            foreach (Role role in this.roleList)//添加所有角色
            {
                component.Create(role);
            }
        }

        IList<User> userList = null;
        private void CreateUser()
        {
            //密码：123;
            userList = new List<User>();//实例化userList集合
            userList.Add(new User() { Account = "admin", Password = "202CB962AC59075B964B07152D234B70", IsActive = true, UserName = "管理员" });//添加管理员
            userList.Add(new User() { Account = "sa", Password = "202CB962AC59075B964B07152D234B70", IsActive = true, UserName = "超级用户" });//添加管理员
            userList.Add(new User() { Account = "test", Password = "202CB962AC59075B964B07152D234B70", IsActive = true, UserName = "测试权限" });//添加一个没有权限的用户
            userList.Add(new User() { Account = "ywy", Password = "202CB962AC59075B964B07152D234B70", IsActive = true, UserName = "业务员" });//添加一个没有权限的用户
            userList.Add(new User() { Account = "ddy", Password = "202CB962AC59075B964B07152D234B70", IsActive = true, UserName = "调度员" });//添加一个没有权限的用户
            userList.Add(new User() { Account = "sj", Password = "202CB962AC59075B964B07152D234B70", IsActive = true, UserName = "司机" });//添加一个没有权限的用户
            userList.Add(new User() { Account = "cw", Password = "202CB962AC59075B964B07152D234B70", IsActive = true, UserName = "财务" });//添加一个没有权限的用户
            userList.Add(new User() { Account = "jc", Password = "202CB962AC59075B964B07152D234B70", IsActive = true, UserName = "决策分析" });//添加一个没有权限的用户

            UserComponent component = new UserComponent();//实例它UserComponent类
            foreach (User user in this.userList)//添加所有用户
            {
                component.Create(user);
            }
        }

        //创建省份
        IList<Province> provinceList = null;
        //创建数据字典
        private void CreateDictionaries()
        {
            provinceList = new List<Province>();
            provinceList.Add(new Province() { Name = "重庆市", Type = DictionaryType.Province });
            provinceList.Add(new Province() { Name = "北京市", Type = DictionaryType.Province });
            provinceList.Add(new Province() { Name = "四川省", Type = DictionaryType.Province });
            foreach (Province d in provinceList)
            {
                Container.Instance.Resolve<IProvinceService>().Create(d);
            }

            //创建费用类型
            IList<BillingItemType> billingItemTypeList = new List<BillingItemType>();
            billingItemTypeList.Add(new BillingItemType() { Name = "路桥费", Type = DictionaryType.BillingItemType });
            billingItemTypeList.Add(new BillingItemType() { Name = "加油费", Type = DictionaryType.BillingItemType });
            billingItemTypeList.Add(new BillingItemType() { Name = "车辆维护费", Type = DictionaryType.BillingItemType });
            billingItemTypeList.Add(new BillingItemType() { Name = "其它可报销费用", Type = DictionaryType.BillingItemType });
            foreach (BillingItemType d in billingItemTypeList)
            {
                Container.Instance.Resolve<IBillingItemTypeService>().Create(d);
            }
        }

        IList<Driver> driverList = null;
        private void CreateDriver()
        {
            driverList = new List<Driver>();
            driverList.Add(new Driver() { UserName = "Driver1", Account = "Driver1", BasicSalary = 3000, Password = "1", IsActive = true });
            driverList.Add(new Driver() { UserName = "Driver2", Account = "Driver2", BasicSalary = 3000, Password = "1", IsActive = true });
            foreach (Driver d in driverList)
            {
                Container.Instance.Resolve<IDriverService>().Create(d);
            }
        }

        IList<Vehicle> vehicleList = null;
        private void CreateVehicle()
        {
            vehicleList = new List<Vehicle>();
            vehicleList.Add(new Vehicle() { VehicleNumber = "渝A23123", ChassisNumber = "D123", DepartmentName = "三五运输", EmptyFuelConsumption = 3, FrameNumber = "2", GenerateNumber = "F4324324", InsuranceNumber = "b000001", LoadCount = 20, LoadedFuelConsumption = 3, VehicleType = "Big", MonthlyDepreciation = 15000 });
            vehicleList.Add(new Vehicle() { VehicleNumber = "渝A23124", ChassisNumber = "D124", DepartmentName = "三五运输", EmptyFuelConsumption = 3, FrameNumber = "2", GenerateNumber = "F4324141324321", InsuranceNumber = "b000002", LoadCount = 20, LoadedFuelConsumption = 3, VehicleType = "Middle", MonthlyDepreciation = 15000 });
            foreach (Vehicle v in vehicleList)
            {
                Container.Instance.Resolve<IVehicleService>().Create(v);
            }
        }

        public string GetDefaultPassword()
        {
            SystemSetting setting = manager.GetAll()[0];//获取系统设置数据，有且只有一条
            return setting.DefaultPassword;//返回系统设置的默认密码
        }


        public new IList<SystemFunction> GetAll()
        {
            return new SystemFunctionManager().GetAll();
        }
    }
}
